package com.example.administrator.foodrowfresh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.foodrowfresh.R;
import com.example.administrator.foodrowfresh.configs.classifyconfing.classifyadapter.AdapterGridView;
import com.example.administrator.foodrowfresh.models.HomepageBody;
import com.example.administrator.foodrowfresh.superBase.MyBaseAdapter;


import java.util.List;

/**
 * Created by Administrator on 2016/5/4.
 */
public class HomepageBodyListViewAdapter extends MyBaseAdapter {

    private List<HomepageBody> dataBeanList;
    private LayoutInflater inflater;
    private GridViewAdapterFive adapterFive;
    private String type;

    public HomepageBodyListViewAdapter(List list, Context mContext) {
        super(list, mContext);
        this.dataBeanList = list;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BodyViewHolder viewHolder = null;
        if (dataBeanList != null) {
            type = dataBeanList.get(position).getType();
            if (convertView == null) {
                if ("3".equals(type) || "4".equals(type) || "5".equals(type) || "2".equals(type)) {
                    convertView = inflater.inflate(R.layout.layout_homepage_body_item_five, parent, false);
                } else if ("1".equals(type)) {
                    //convertView = inflater.inflate(R.layout.layout_image_one, parent, false);
                }
                viewHolder = new BodyViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (BodyViewHolder) convertView.getTag();
            }
            if ("3".equals(type) || "4".equals(type) || "5".equals(type) || "2".equals(type)) {
                viewHolder.listTextView.setText(dataBeanList.get(position).getTitle());
                viewHolder.reightTextView.setText(dataBeanList.get(position).getDescription());
                List<HomepageBody.FloorItemBean> floorItem = dataBeanList.get(position).getFloorItem();
                adapterFive = new GridViewAdapterFive(floorItem, parent.getContext(), dataBeanList.get(position).getType());
                viewHolder.gridViewfive.setAdapter(adapterFive);
                AdapterGridView.setGridViewHeightBasedOnChildren(viewHolder.gridViewfive);
            } else if ("1".equals(type)) {
                //x.image().bind(viewHolder.imageViewOne, dataBeanList.get(position).getFloorItem().get(position).getUrl());
            }
        }
        return convertView;
    }

    public class BodyViewHolder {
        TextView listTextView;
        TextView reightTextView;
        GridView gridViewfive;

        ImageView imageViewOne;

        public BodyViewHolder(View convertView) {
            super();
            if ("3".equals(type) || "5".equals(type)) {
                setView(convertView);
                gridViewfive.setNumColumns(2);
            } else if ("1".equals(type)) {
                //imageViewOne = (ImageView) convertView.findViewById(R.id.image_one);
                //   break;
            } else if ("4".equals(type)) {
                setView(convertView);
                gridViewfive.setNumColumns(3);
            }else if ("2".equals(type))
            {
                setView(convertView);
                gridViewfive.setNumColumns(1);
            }
        }

        /*
        * 复用
        * */
        public void setView(View convertView) {
            listTextView = (TextView) convertView.findViewById(R.id.lift_title);
            reightTextView = (TextView) convertView.findViewById(R.id.reigth_title);
            gridViewfive = (GridView) convertView.findViewById(R.id.gridview_homepage_five);
        }
    }

}
